package il.ac.braude.sona.control;

import il.ac.braude.sona.database.User;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.server.Server;
import il.ac.braude.sona.strings.Resources;
import il.ac.braude.sona.table.ChangeRequestTableFrame;
import il.ac.braude.sona.ui.skeleton.MainMenuFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;

public class MainFlowControl implements ActionListener {
	private User user;
	private ChangeRequestController reqHandler = new ChangeRequestController();
	private LoginController loginHandler = new LoginController();
	private MainMenuFrame mainMenu; 
	public void go() throws InterruptedException, SQLException {
		try {
			Client.connect("5.29.145.224", 12389);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "fatal : couldnt reach server");
			return;
		}
		user=loginHandler.login();
		mainMenu=new MainMenuFrame(this,user);
		mainMenu.showMenu();	
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if (((JButton)e.getSource()).getText().equals(Resources.Strings.CreateChangeReq))
			reqHandler.execCreateChangeRequest();
		if (((JButton)e.getSource()).getText().equals(Resources.Strings.ViewRequestByID))
			reqHandler.execViewChangeRequest();
		if (((JButton)e.getSource()).getText().equals(Resources.Strings.ViewAllRequests))
			reqHandler.execViewAllChangeRequests();
	}
	/**
	 * @param args
	 * @throws InterruptedException 
	 * @throws SQLException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws InterruptedException, SQLException, IOException {
		new MainFlowControl().go();
	}

}
